home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / mm-0.90 / compat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  6.2 KB  |  323 lines

  1. /*
  2.  * Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  * the City of New York.  Permission is granted to any individual or
  4.  * institution to use, copy, or redistribute this software so long as it
  5.  * is not sold for profit, provided this copyright notice is retained.
  6.  */
  7.  
  8. #ifndef lint
  9. static char *rcsid = "$Header: /f/src2/encore.bin/cucca/mm/tarring-it-up/RCS/compat.c,v 2.4 90/10/04 18:23:46 melissa Exp $";
  10. #endif
  11.  
  12. #include "config.h"
  13. #include "osfiles.h"
  14. #include "compat.h"
  15.  
  16. /*
  17.  * If getwd is a macro, we need to supply our own getwd() routine.
  18.  */
  19.  
  20. #ifdef getwd
  21. char *
  22. getwd (buf)
  23. char *buf;
  24. {
  25. #ifdef HAVE_GETCWD
  26.     extern int sys_nerr, errno;
  27.     extern char *sys_errlist[];
  28.  
  29.     if (getcwd (buf, MAXPATHLEN) == 0) {
  30.     if (errno > 0 && errno <= sys_nerr)
  31.         strcpy (buf, sys_errlist[errno]);
  32.     else
  33.         strcpy (buf, "getcwd failed");
  34.     return (char *) 0;
  35.     }
  36. #else
  37.     char *evalpipe ();
  38.  
  39.     if (evalpipe("/bin/pwd", buf, MAXPATHLEN) != buf) {
  40.     strcpy (buf, "/bin/pwd failed");
  41.     return 0;
  42.     }
  43. #endif
  44.     return buf;
  45. }
  46. #endif
  47.  
  48. /*
  49.  * If "rename" is a macro, we need to supply our own rename() routine.
  50.  * This version is careful not to delete the source file; if your kernel
  51.  * has a rename(2) that can unlink the source file accidentally, or may
  52.  * succeed without effectively changing the name of the file, undefine
  53.  * HAVE_RENAME in config.h and use this one instead.
  54.  */
  55. #ifdef rename
  56. rename (from, to)
  57. char *from, *to;
  58. {
  59.     struct stat s_from, s_to;
  60.  
  61.     /*
  62.      * make sure file exists, and get the inode info
  63.      */
  64.     if (stat (from, &s_from) != 0)
  65.     return -1;
  66.     
  67.     /*
  68.      * short circuit attempts to link/unlink directory files
  69.      * in case we're running as root.
  70.      */
  71.     if ((s_from.st_mode & S_IFMT) != S_IFREG) {
  72.     errno = EINVAL;
  73.     return -1;
  74.     }
  75.  
  76.     /*
  77.      * make sure that the source != destination, and
  78.      * that the destination is not a directory.
  79.      */
  80.     if (stat (to, &s_to) == 0) {
  81.     if (s_to.st_ino == s_from.st_ino ||
  82.         (s_to.st_mode & S_IFMT) != S_IFREG) {
  83.         errno = EINVAL;
  84.         return -1;
  85.     }
  86.     if (unlink (to) != 0)
  87.         return -1;
  88.     }
  89.  
  90.     if (link (from, to) != 0)        /* make the link */
  91.     return -1;
  92.  
  93.     if (unlink (from) != 0)        /* delete the source file */
  94.     return -1;
  95.  
  96.     return 0;
  97. }
  98. #endif
  99.  
  100. /*
  101.  * lock/unlock a file.
  102.  */
  103.  
  104. lock_file (name, fd)
  105. char *name;
  106. int fd;
  107. {
  108.     int status;
  109. #ifdef HAVE_F_SETLK
  110.     struct flock flk;
  111.     flk.l_type = F_WRLCK;
  112.     flk.l_whence = 0;
  113.     flk.l_start = 0;
  114.     flk.l_len = 0;
  115.     status = fcntl (fd, F_SETLK, &flk);
  116. #else
  117. #ifdef HAVE_FLOCK
  118.     status = flock (fd, LOCK_NB|LOCK_EX);
  119. #else
  120. #ifdef HAVE_LOCKF
  121.     status = lockf (fd, F_TLOCK, 0);
  122. #else
  123.     return (-1);            /* pretend we locked it */
  124. #ifdef undef
  125.     int lockfd;
  126.     char lockfname[MAXPATHLEN];
  127.  
  128.     sprintf(lockfname, "%s.lock", name); /* XXX filename may exceed 14 chars */
  129.  
  130.     if ((lockfd = open(lockfname, O_CREAT|O_EXCL|O_WRONLY, 0)) >= 0) {
  131.     status = 0;
  132.     (void) close(lockfd);
  133.     } else {
  134.     status = -1;
  135.     if (errno != EEXIST)
  136.         perror(lockfname);
  137.     }
  138. #endif /* undef */
  139. #endif /* HAVE_LOCKF */
  140. #endif /* HAVE_FLOCK */
  141. #endif /* HAVE_F_SETLK */
  142.     return status;
  143. }
  144.  
  145. unlock_file (name, fd)
  146. char *name;
  147. int fd;
  148. {
  149.     int status;
  150. #ifdef HAVE_F_SETLK
  151.     struct flock flk;
  152.     flk.l_type = F_UNLCK;
  153.     flk.l_whence = 0;
  154.     flk.l_start = 0;
  155.     flk.l_len = 0;
  156.     status = fcntl (fd, F_SETLK, &flk);
  157. #else
  158. #ifdef HAVE_FLOCK
  159.     status = flock (fd, LOCK_UN);
  160. #else
  161. #ifdef HAVE_LOCKF
  162.     status = lockf (fd, F_ULOCK, 0);
  163. #else
  164.     return (-1);            /* pretend success */
  165. #ifdef undef
  166.     char lockfname[MAXPATHLEN];
  167.  
  168.     sprintf(lockfname, "%s.lock", name);
  169.     status = unlink(lockfname);
  170. #endif /* undef */
  171. #endif /* HAVE_LOCKF */
  172. #endif /* HAVE_FLOCK */
  173. #endif /* HAVE_F_SETLK */
  174.     return status;
  175. }
  176.  
  177. /* 
  178.  * Return the "official" name of the local host.
  179.  */
  180.  
  181. #ifndef MAXHOSTNAMELEN
  182. #define MAXHOSTNAMELEN 64
  183. #endif
  184.  
  185. #ifdef HAVE_GETHOSTBYNAME
  186. #include <netdb.h>
  187. #endif
  188.  
  189. #ifdef HAVE_UNAME
  190. #include <sys/utsname.h>
  191. #endif
  192.  
  193. char shorthostname[MAXHOSTNAMELEN];
  194. char fullhostname[MAXHOSTNAMELEN];
  195. char *mailhostname = 0;
  196. char *localdomain = 0;
  197.  
  198. char *
  199. getlocalhostname ()
  200. {
  201.     char *cp;
  202.     char *index ();
  203.  
  204. #ifdef HAVE_GETHOSTNAME
  205.     gethostname (shorthostname, sizeof shorthostname - 1);
  206. #else
  207. #ifdef HAVE_UNAME
  208.     struct utsname uts;
  209.     uname (&uts);
  210.     strcpy (shorthostname, uts.nodename);
  211. #else
  212. #ifdef PHOSTNAME
  213.     char *evalpipe ();
  214.  
  215.     if (evalpipe (PHOSTNAME, shorthostname, sizeof shorthostname) == NULL)
  216.     shorthostname[0] = 0;
  217. #else
  218. #ifdef HOSTNAME
  219.     strncpy (shorthostname, HOSTNAME, MAXHOSTNAMELEN-1);
  220. #endif
  221. #endif
  222. #endif
  223. #endif
  224.     /* 
  225.      * See if the hostname already has a domain tacked on
  226.      */
  227.     if (cp = index (shorthostname, '.')) {
  228.     strcpy (fullhostname, shorthostname);
  229.     *cp++ = 0;
  230.     localdomain = fullhostname + (cp - shorthostname);
  231.     }
  232.     else
  233.     localdomain = 0;
  234.  
  235.     if (!localdomain) {
  236. #ifdef HAVE_GETHOSTBYNAME
  237.     struct hostent *hp = gethostbyname (shorthostname);
  238.     if (hp) {
  239.         cp = index (hp->h_name, '.');
  240.         if (cp) {
  241.         strcpy (fullhostname, hp->h_name);
  242.         localdomain = fullhostname + (cp - hp->h_name + 1);
  243.         }
  244.     }
  245. #endif
  246.     }
  247.  
  248.     if (!localdomain) {
  249. #ifdef LOCALDOMAIN
  250.     sprintf (fullhostname, "%s%s", shorthostname, LOCALDOMAIN);
  251.     localdomain = fullhostname + strlen (shorthostname) + 1;
  252. #else
  253.     strcpy (fullhostname, shorthostname);
  254. #endif
  255.     }
  256. #ifdef HIDDENNET
  257.     mailhostname = HIDDENNET;
  258. #else
  259.     mailhostname = fullhostname;
  260. #endif
  261.     return fullhostname;
  262. }
  263.  
  264. char *
  265. evalpipe (cmd, result, maxlen)
  266. char *cmd, *result;
  267. int maxlen;
  268. {
  269.     char *cp;
  270.     FILE *fp, *mm_popen ();
  271.     
  272.     if (fp = mm_popen (cmd, "r")) {
  273.     if (fgets (result, maxlen, fp) == result) {
  274.         if (cp = index (result, '\n'))
  275.         *cp = 0;
  276.         else
  277.         result = 0;
  278.         (void) mm_pclose (fp);
  279.         return result;
  280.     }
  281.     else
  282.         result = NULL;
  283.     (void) mm_pclose (fp);
  284.     }
  285.     return result;
  286. }
  287.  
  288. #ifdef read
  289. #undef read
  290. sys_read (fd, buf, count)
  291. int fd, count;
  292. char *buf;
  293. {
  294.     int n;
  295.     
  296.     while ((n = read (fd, buf, count)) < 0 && errno == EINTR)
  297.     ;
  298.     return n;
  299. }
  300. #endif
  301.  
  302. #ifdef write
  303. #undef write
  304. sys_write (fd, buf, count)
  305. int fd, count;
  306. char *buf;
  307. {
  308.     
  309.     int n, left = count;
  310.  
  311.     for (left = count; left > 0; left -= n) {
  312.     n = write (fd, buf, count);
  313.     if (n < 0)
  314.         if (errno != EINTR)
  315.         return n;
  316.         else
  317.         n = 0;
  318.     }
  319.  
  320.     return (count - left);
  321. }
  322. #endif
  323.